using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Lib.Utilities
{
    class BasicHTTP
    {
        public BasicHTTP()
        {

        }
        /// <summary>
        /// Get Link status code from URL
        /// </summary>
        /// <param name="URL">The link to check</param>
        /// <returns>HttpStatusCode of the URL</returns>
        public HttpStatusCode GetLinkStatus(string URL)
        {
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);
            objRequest.AllowAutoRedirect = true;
            objRequest.AllowWriteStreamBuffering = true;
            //objRequest.Timeout = 60;
            objRequest.KeepAlive = true;
            //objRequest.AutomaticDecompression = true;
            objRequest.ContentType = "text/html; charset=UTF-8";
            objRequest.CookieContainer = new CookieContainer();

            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            return objResponse.StatusCode;
        }
        /// <summary>
        /// Get HTML Content from the URL, return as string
        /// </summary>
        /// <param name="URL">The link to check</param>
        /// <returns>string HTML Content of the URL</returns>
        public string GetLinkContent(string URL)
        {
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);
            objRequest.AllowAutoRedirect = true;
            objRequest.AllowWriteStreamBuffering = true;
            //objRequest.Timeout = 60;
            objRequest.KeepAlive = true;
            //objRequest.AutomaticDecompression = true;
            objRequest.ContentType = "text/html; charset=UTF-8";
            objRequest.CookieContainer = new CookieContainer();
            
            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            if (objResponse.StatusCode == HttpStatusCode.OK)
            {
                StreamReader sr = new StreamReader(objResponse.GetResponseStream());
                return sr.ReadToEnd();
            }
            else
                return null;
        }
        /// <summary>
        /// Get HttpStatusCode and return the HTML Content through string Content
        /// </summary>
        /// <param name="URL">The link to get</param>
        /// <param name="Content">string variable to contain the returned HTML</param>
        /// <returns>HttpStatusCode of the URL</returns>
        public HttpStatusCode GetLinkStatusAndContent(string URL, ref string Content)
        {
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);
            objRequest.AllowAutoRedirect = true;
            objRequest.AllowWriteStreamBuffering = true;
            //objRequest.Timeout = 60;
            objRequest.KeepAlive = true;
            //objRequest.AutomaticDecompression = true;
            objRequest.ContentType = "text/html; charset=UTF-8";
            objRequest.CookieContainer = new CookieContainer();
            try
            {
                HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
                if (objResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader sr = new StreamReader(objResponse.GetResponseStream());
                    Content = sr.ReadToEnd();
                }
                return objResponse.StatusCode;
            }
            catch
            {
                retry += 1;
                if (retry == 3)
                {
                    retry = 0;
                    return HttpStatusCode.BadRequest;
                }
                return GetLinkStatusAndContent(URL, ref Content);
            }
        }
        int retry = 0;
    }
}
